package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.Member;
import com.example.buildtable.pojo.PaymentMethod;
import com.example.common.util.R;
import com.example.buildtable.pojo.Recharge;
import com.example.buildtable.service.RechargeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "充值")
@RequestMapping(value = "/recharge/")
public class RechargeController {
    @Autowired
    private RechargeService rechargeService;
    
    @Operation(summary = "查询所有充值数据")
    @GetMapping(value = "/getAllRecharge")
    public R getAllRecharge() {
        LambdaJoinQueryWrapper<Recharge> wrapper = new LambdaJoinQueryWrapper<>(Recharge.class);
        wrapper.innerJoin(Member.class, Member::getMemberId, Recharge::getRechargeMemberId)
                .innerJoin(PaymentMethod.class, PaymentMethod::getPaymentMethodId, Recharge::getPaymentMethodId);
        List<Recharge> rechargeList = rechargeService.list(wrapper);
        return R.ok("查询所有充值数据成功").data("rechargeList", rechargeList);
    }
    
    @Operation(summary = "增加充值")
    @PostMapping(value = "/addRecharge")
    public R addRecharge(Recharge recharge) {
        boolean flag = rechargeService.save(recharge);
        if (flag) {
            return R.ok("充值成功");
        } else {
            return R.ok("充值失败");
        }
    }

    @Operation(summary = "删除充值")
    @GetMapping(value = "/deleteRecharge/{rechargeId}")
    public R deleteRecharge(@PathVariable("rechargeId") Integer rechargeId) {
        boolean flag = rechargeService.removeById(rechargeId);
        if (flag) {
            return R.ok("删除充值成功");
        } else {
            return R.ok("删除充值失败");
        }
    }

    @Operation(summary = "按照充值查看")
    @GetMapping(value = "/getRechargeById/{rechargeId}")
    public R getRechargeById(@PathVariable("rechargeId") Integer rechargeId) {
        LambdaJoinQueryWrapper<Recharge> wrapper = new LambdaJoinQueryWrapper<>(Recharge.class);
        wrapper.innerJoin(Member.class, Member::getMemberId, Recharge::getRechargeMemberId)
                .innerJoin(PaymentMethod.class, PaymentMethod::getPaymentMethodId, Recharge::getPaymentMethodId);
        wrapper.eq(Recharge::getRechargeId, rechargeId);
        Recharge recharge = rechargeService.getOne(wrapper);
        if (recharge != null) {
            return R.ok("按照充值查看成功").data("recharge", recharge);
        } else {
            return R.error("按照充值查看失败");
        }
    }

    @Operation(summary = "修改充值")
    @PostMapping(value = "/updateRecharge")
    public R updateRecharge(Recharge recharge) {
        boolean flag = rechargeService.updateById(recharge);
        if (flag) {
            return R.ok("修改充值成功");
        } else {
            return R.ok("修改充值失败");
        }
    }
}